package com.hdyg.sixg.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.adapter.AreaAdapter;
import com.hdyg.sixg.adapter.CoinAdapter;
import com.hdyg.sixg.app.MvpApplication;
import com.hdyg.sixg.entry.BibiBean;
import com.hdyg.sixg.mvp.Contrant.CFragBibi;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PFragBibi;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 币币
 * @time 2019/4/4 9:25
 */
public class BiBiFragment extends BaseFragment implements CFragBibi.IVFragBibi {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_left)
    SuperTextView svLeft;
    @BindView(R.id.sv_right)
    SuperTextView svRight;
    @BindView(R.id.sv_real_price)
    SuperTextView svRealPrice;
    @BindView(R.id.sv_current_balance)
    SuperTextView svCurrentBalance;
    @BindView(R.id.et_exchange_number)
    EditText etExchangeNumber;
    @BindView(R.id.sv_handing_fee)
    SuperTextView svHandingFee;
    @BindView(R.id.sv_expect_echange)
    SuperTextView svExpectEchange;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CFragBibi.IPFragBibi mPresenter;
    private List<BibiBean.DataBean> coinBeans;
    private BibiBean bibiBean;
    private String fee, coin_type, coin_price, coin_name, convert_number, transPwd;
    private PopupWindow mPopupWindow;


    @Override
    protected void createPresenter() {
        mPresenter = new PFragBibi(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bibi;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.bibi_title);
        llTopLeft.setVisibility(View.GONE);
        onClickListener();
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.COINS_INIT_URL, GetParamUtil.getBibiInitParam());
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        etExchangeNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                convert_number = s.toString();
                setHanFeeAndExchange();
            }
        });
        //币种选择
        svRight.setOnSuperTextViewClickListener(superTextView -> {
            showAreaPop(superTextView);
        });
        //确定交易
        btSure.setOnClickListener(v -> {
            convert_number = etExchangeNumber.getText().toString();
            transPwd = etPayPwd.getText().toString();
            if (TextUtils.isEmpty(convert_number)) {
                toastNotifyShort(R.string.bibi_exchange_hint);
                return;
            }
            if (TextUtils.isEmpty(coin_type)) {
                toastNotifyShort(R.string.toast_coin);
                return;
            }
            if (TextUtils.isEmpty(transPwd)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyTrans(BaseUrl.TRANS_COINS_URL, GetParamUtil.getBibiTransParam(coin_type, convert_number, transPwd));
        });

    }

    private void setHanFeeAndExchange() {
        if (!TextUtils.isEmpty(convert_number)) {
            //手续费 = 兑换量 * 手续费率
            //预计兑换 = 兑换6G数量 * 6g链价格  / 数字货币价格
            double str = Double.valueOf(convert_number) * Double.valueOf(fee) / 100;
            double str2 = Double.valueOf(convert_number) * Double.valueOf(bibiBean.getSix_price()) / Double.valueOf(coin_price);
            svHandingFee.setRightString(StringUtil.dealFloatStr(str));
            svExpectEchange.setRightString(StringUtil.dealFloatStr(str2));
        }else {
            svHandingFee.setRightString("");
            svExpectEchange.setRightString("");
        }
    }

    private void showAreaPop(View v) {
        if (coinBeans == null) {
            toastNotifyShort(R.string.toast_coin);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_coin, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_coin);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        CoinAdapter areaAdapter = new CoinAdapter(R.layout.item_coin, coinBeans);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            coin_type = coinBeans.get(position).getCoin_type();
            coin_price = coinBeans.get(position).getPrice();
            coin_name = coinBeans.get(position).getEn_name();
            svRight.setCenterString(coin_name);
            String coinPrice = String.format(getResources().getString(R.string.bibi_real_price), coin_price);
            svRealPrice.setRightString(coinPrice);
            setHanFeeAndExchange();
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.WRAP_CONTENT)
                .showAsDropDown(mContext, v, 0);

    }

    @Override
    public void vGetInitSuccess(BibiBean dataBean) {
        bibiBean = dataBean;
        coinBeans = dataBean.getCoins();
        coin_type = dataBean.getCoins().get(0).getCoin_type();
        coin_price = dataBean.getCoins().get(0).getPrice();
        coin_name = dataBean.getCoins().get(0).getEn_name();
        String sixPrice = String.format(getResources().getString(R.string.bibi_real_price), dataBean.getSix_price());
        String coinPrice = String.format(getResources().getString(R.string.bibi_real_price), coin_price);
        svRealPrice.setLeftString(sixPrice).setRightString(coinPrice);
        svCurrentBalance.setRightString(dataBean.getBalance());
        svRight.setCenterString(coin_name);
        fee = dataBean.getFee();
    }

    @Override
    public void vGetApplyTransSuccess(String dataBean) {
        initData();
        toastNotifyShort(dataBean);
        svHandingFee.setRightString("");
        svExpectEchange.setRightString("");
        etExchangeNumber.setText("");
        etPayPwd.setText("");

    }
}
